""" System imports """
import os,sys
import logging

""" Other modules """
import pygame

""" Game """
#import resourcemanager
from constants import *
from widget import Widget
import font
import textwrap

""" Constants """

""" Set up logging """
label_logger = logging.getLogger("epicquest.label")
label_logger.debug("Initializing Label")

""" A box with some text in it
"""
class Label(Widget):

    """
    All params are keyword args. If you don't set one, it will choose a default.
    @see: widget.Widget for base class params
    @param text: The text to show in the label
    @param font: The font to use (defaults to NESFONT)   
    @param left_padding: Left text padding
    @param top_padding: Top text padding 
    @param wrap_text: True to wrap text 
    """
    def __init__(self, *args, **config):
        Widget.__init__(self, *args, **config)
        self.text = config.get("text", "")
        self.font = config.get("font", font.Font(NES_FONT, (255, 255, 255)))
        self.left_padding = config.get("left_padding", 2)
        self.top_padding = config.get("top_padding", 2)
        self.wrap_text = config.get("wrap_text", False)
        
        self.font_surf = pygame.surface.Surface(self.rect.size)
        self.font_surf = self.font.render(self.text)
        

    def update(self):
        if self.dirty == 0:
            return
        Widget.update(self)
        
        if self.wrap_text:
            width = (self.rect.width - self.left_padding) / self.font.get_width()
            lines = textwrap.wrap(self.text)
            i = 0
            for line in lines:
                wrap_surf = pygame.surface.Surface(self.rect.size)
                wrap_surf = self.font.render(self.text)
                self.font_surf.blit(wrap_surf,(0,i))
                i = i + self.font.get_height() + 1 
                
        self.surface.blit(self.font_surf, (self.left_padding,self.top_padding))
        self.dirty = 0
    
    def setText(self, text):
        self.text = text       
        self.dirty = 1
        